<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://java.sun.com/jsf/facelets"
                template="/resources/template/template.xhtml"
                xmlns:h="http://java.sun.com/jsf/html"
                xmlns:p="http://primefaces.org/ui"
                xmlns:f="http://java.sun.com/jsf/core"
                xmlns:pharmacy="http://java.sun.com/jsf/composite/pharmacy">

    <ui:define name="content">

        <h:outputStylesheet library="css" name="maincss.css" ></h:outputStylesheet>

        <h:form id="bill">
            <p:panel rendered="#{!pharmacyPurchaseController.printPreview}">
                <f:facet name="header" > 
                    <h:outputText value="Opening Stock Purchase For Wholesale" />                     
                </f:facet>

                <h:panelGrid columns="7"  >
                    <p:selectOneMenu   id="cmbPs" value="#{pharmacyPurchaseController.bill.paymentMethod}">                                                                     
                        <f:selectItems value="#{enumController.paymentMethodsForPo}" />
                    </p:selectOneMenu>
                    <p:outputLabel value="Supplier"/>
                    <p:autoComplete converter="deal" value="#{pharmacyPurchaseController.bill.fromInstitution}" 
                                    forceSelection="true"
                                    completeMethod="#{dealerController.completeDealor}" var="vt" itemLabel="#{vt.name}" itemValue="#{vt}" />
                    <h:outputLabel value="Purchasing Institution"/>
                    <p:autoComplete converter="institutionConverter" value="#{pharmacyPurchaseController.bill.referenceInstitution}"
                                    completeMethod="#{institutionController.completeCompany}" 
                                    forceSelection="true"
                                    var="vt" itemLabel="#{vt.name}" itemValue="#{vt}" />
                    <p:commandButton  value="Settle" action="#{pharmacyPurchaseController.settle}"
                                      onclick="onSubmitButton();"
                                    ajax="false"  style="width: 150px; padding: 1px;border: 1px solid ; margin: auto;">
                    </p:commandButton>
                    <p:commandButton value="New Bill" ajax="false"
                                     onclick="onSubmitButton();"
                                    action="#{pharmacyPurchaseController.makeNull()}" >
                    </p:commandButton>

                </h:panelGrid>

                <p:commandButton id="nullButton" value="No Action" action="#" style="display: none;" ></p:commandButton>

                <p:defaultCommand  target="btnAdd" />  

                <h:panelGrid id="itemselectgrid" columns="2" style="min-width: 100%;">
                    <p:focus id="focusItem" for="acItem" ></p:focus>
                    <p:panelGrid id="itemSelectgrid1" columns="8">                       
                        <h:outputLabel value="Select Item"/>
                        <h:outputLabel value="Qty"/>
                        <h:outputLabel value="Sale Rate"></h:outputLabel>
                        <h:outputLabel value="Purchase Rate"></h:outputLabel>
                        <h:outputLabel value="Wholesale Rate"></h:outputLabel>
                        <h:outputLabel value="DOE(dd mm yy)"/>
                        <h:outputLabel value="Bathch No"/>

                        <h:outputLabel/>

                        <p:autoComplete id="acItem"   value="#{pharmacyPurchaseController.currentBillItem.item}" 
                                        forceSelection="true"
                                        completeMethod="#{itemController.completeAmpItem}" 
                                        var="vt" itemLabel="#{vt.name}" itemValue="#{vt}" >
                            <p:column headerText="Item" >
                                <h:outputLabel value="#{vt.name}"></h:outputLabel>
                            </p:column>
                            <p:column headerText="Code" >
                                <h:outputLabel value="#{vt.code}"></h:outputLabel>
                            </p:column>
                            <p:column headerText="Pack Size" >
                                <p:outputLabel value=" X #{vt.dblValue}" 
                                               rendered="#{vt.class eq 'class com.divudi.entity.pharmacy.Ampp'}"/>                            
                            </p:column>
                            <p:column headerText="Pack Size" >
                                <p:outputLabel value=" X #{vt.dblValue}" 
                                               rendered="#{vt.class eq 'class com.divudi.entity.pharmacy.Ampp'}"/>                            
                            </p:column>
                            <p:column headerText="Pack Size" >
                                <p:outputLabel value=" X #{vt.dblValue}" 
                                               rendered="#{vt.class eq 'class com.divudi.entity.pharmacy.Ampp'}"/>                            
                            </p:column>

                        </p:autoComplete>
                        <p:inputText autocomplete="off" id="txtQty" styleClass="numericTxt" value="#{pharmacyPurchaseController.currentBillItem.tmpQty}" style="width:100%" />  

                        <p:inputText autocomplete="off" id="tmpRetail"  styleClass="numericTxt" 
                                     value="#{pharmacyPurchaseController.currentBillItem.pharmaceuticalBillItem.retailRate}" >
                            <f:ajax event="keyup" execute="@this" render="txtPrate txtWholesale" listener="#{pharmacyPurchaseController.calculatePurchaseRateAndWholesaleRateFromRetailRate}" ></f:ajax>
                        </p:inputText>  

                        <p:inputText autocomplete="off" id="txtPrate"  styleClass="numericTxt" 
                                     value="#{pharmacyPurchaseController.currentBillItem.pharmaceuticalBillItem.purchaseRate}"  >
                        </p:inputText>

                        <p:inputText autocomplete="off" id="txtWholesale"  styleClass="numericTxt" 
                                     value="#{pharmacyPurchaseController.currentBillItem.pharmaceuticalBillItem.wholesaleRate}"  >
                        </p:inputText>

                        <p:calendar navigator="true"
                                    id="calDoe" pattern="dd MM yy" value="#{pharmacyPurchaseController.currentBillItem.pharmaceuticalBillItem.doe}"   >
                            <f:ajax event="dateSelect" execute="@this" render="tmp" listener="#{pharmacyPurchaseController.setBatch()}"/>
                        </p:calendar> 
                        <p:inputText autocomplete="off"  styleClass="numericTxt" 
                                     id="tmp" value="#{pharmacyPurchaseController.currentBillItem.pharmaceuticalBillItem.stringValue}"  />  
                        <p:commandButton id="btnAdd" value="Add Item" 
                                        action="#{pharmacyPurchaseController.addItem}" 
                                         onclick="onSubmitButton();"
                                         process="itemselectgrid" update="itemList itemselectgrid tot focusItem :#{p:component('total')}"/>
                    </p:panelGrid>

                </h:panelGrid>

                <p:dataTable styleClass="noBorder" var="ph" value="#{pharmacyPurchaseController.billItems}" id="itemList">  
                    <f:facet name="header">  
                        Purchased Items
                    </f:facet>  

                    <p:column headerText="Item Name" >  
                        <h:outputLabel id="item" value="#{ph.item.name} - #{ph.item.code}" style="width:100%">
                        </h:outputLabel>
                    </p:column>  

                    <p:column headerText="Qty" > 
                        <h:outputLabel value="#{ph.tmpQty}" style="width:100%">
                        </h:outputLabel>
                    </p:column>  

                    <p:column headerText="P. Rate" >  
                        <h:outputLabel value="#{ph.pharmaceuticalBillItem.purchaseRate}" style="width:100%">
                            <f:convertNumber pattern="0.00" ></f:convertNumber>
                        </h:outputLabel>
                    </p:column>              

                    <p:column headerText="S. Rate" > 
                        <h:outputLabel value="#{ph.pharmaceuticalBillItem.retailRate}" style="width:100%">
                            <f:convertNumber pattern="0.00" ></f:convertNumber>
                        </h:outputLabel>
                    </p:column>  

                    <p:column headerText="WS Rate" >                   
                        <h:outputLabel value="#{ph.pharmaceuticalBillItem.wholesaleRate}" style="width:100%">
                            <f:convertNumber pattern="0.00" ></f:convertNumber>
                        </h:outputLabel>
                    </p:column>  


                    <p:column headerText="Value" >  
                        <h:outputText id="total" value="#{ph.pharmaceuticalBillItem.purchaseRate*ph.pharmaceuticalBillItem.qty}" >
                            <f:convertNumber pattern="0.00" ></f:convertNumber>
                        </h:outputText>
                    </p:column>  


                    <p:column headerText="Batch" >                   
                        <h:outputText  id="txtBatch" value="#{ph.pharmaceuticalBillItem.stringValue}" >
                        </h:outputText>
                    </p:column>  

                    <p:column headerText="DoE" >
                        <h:outputText   value="#{ph.pharmaceuticalBillItem.doe}" >
                            <f:convertDateTime pattern="dd MMM yyyy" ></f:convertDateTime>
                        </h:outputText>
                    </p:column>

                    <p:column headerText="R. Margin" >                    
                        <h:outputText value="#{(ph.pharmaceuticalBillItem.retailRate - ph.pharmaceuticalBillItem.purchaseRate ) / 100}" >
                            <f:convertNumber pattern="0.00" ></f:convertNumber>
                        </h:outputText>
                    </p:column>
                    
                    <p:column headerText="W. Margin" >                    
                        <h:outputText value="#{(ph.pharmaceuticalBillItem.wholesaleRate - ph.pharmaceuticalBillItem.purchaseRate ) / 100}" >
                            <f:convertNumber pattern="0.00" ></f:convertNumber>
                        </h:outputText>
                    </p:column>

                    <p:column >
                        <p:commandButton value="Remove" ajax="false" action="#{pharmacyPurchaseController.removeItem(ph)}"/>
                    </p:column>

                </p:dataTable>  

                <p:panel>
                    <p:panelGrid columns="2" style="min-width: 100%">
                        <p:panelGrid columns="2">
                            <p:outputLabel value="Invoice No : "/>
                            <p:inputText autocomplete="off" value="#{pharmacyPurchaseController.bill.invoiceNumber}" />
                            <p:outputLabel value="Invoice Date"/>
                            <p:calendar  value="#{pharmacyPurchaseController.bill.invoiceDate}"   navigator="true" pattern="dd/MM/yy" /> 
                        </p:panelGrid>
                        <p:panelGrid columns="2" id="tot" style="min-width: 100%">
                            <p:outputLabel value="Gross Total"/>
                            <p:outputLabel id="gro"  value="#{pharmacyPurchaseController.bill.total}" >
                                <f:convertNumber pattern="#,##0.00" ></f:convertNumber>
                            </p:outputLabel>
                            <p:outputLabel value="Tax"/>
                            <p:inputText autocomplete="off" id="tx" value="#{pharmacyPurchaseController.bill.tax}" > 
                                <p:ajax process="gro tx dis" update="net" event="keyup"/>
                            </p:inputText>
                            <p:outputLabel value="Discount"/>
                            <p:inputText autocomplete="off" id="dis" value="#{pharmacyPurchaseController.bill.discount}" >
                                <p:ajax process="gro tx dis" update="net" event="keyup"/>
                            </p:inputText>
                            <p:outputLabel value="Net Total"/>
                            <h:outputLabel id="net"  value="#{pharmacyPurchaseController.bill.netTotal}" > 
                                <f:convertNumber pattern="#,##0.00"/> 
                            </h:outputLabel>
                        </p:panelGrid>
                    </p:panelGrid>
                </p:panel>
            </p:panel>

            <p:panel rendered="#{pharmacyPurchaseController.printPreview}">
                <p:commandButton ajax="false" 
                                 onclick="onSubmitButton();"
                                action="#{pharmacyPurchaseController.makeNull()}" value="New Bill"/>                    
                <p:commandButton value="Print" ajax="false" action="#" >
                    <p:printer target="gpBillPreview" ></p:printer>
                </p:commandButton>
                <p:panel id="gpBillPreview">
                    <pharmacy:purhcase bill="#{pharmacyPurchaseController.bill}"/>
                </p:panel>
            </p:panel>
        </h:form>
    </ui:define>  

</ui:composition>
